package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.query.EmployeeQueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.util.JsonResult;
import cn.wolfcode.wms.util.RequiredPermission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	private IEmployeeService service;
	@Autowired
	private IDepartmentService departmentService;
	@Autowired
	private IRoleService roleService;
	@RequiredPermission("员工列表")
	@RequestMapping("/list")
	@RequiresPermissions("employee:list")
	public String list(Model model,@ModelAttribute("qo")EmployeeQueryObject qo){
		model.addAttribute("departments", departmentService.list());
		model.addAttribute("result", service.query(qo));
		return "employee/list";
	}
	@RequiredPermission("员工删除")
	@RequestMapping("/delete")
	@ResponseBody
	@RequiresPermissions("employee:delete")
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.delete(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}
		return result;
	}
	
	@RequiredPermission("员工编辑")
	@RequestMapping("/input")
	@RequiresPermissions("employee:input")
	public String input(Long id,Model model){
		model.addAttribute("roles", roleService.list());
		model.addAttribute("departments", departmentService.list());
		if (id!=null) {
			model.addAttribute("employee", service.get(id));
		}
		return "employee/input";
	}
	@RequiredPermission("员工保存或更新")
	@RequestMapping("/saveOrUpdate")
	@RequiresPermissions("employee:saveOrUpdate")
	public String saveOrUpdate(Employee employee,Long[] ids){
		if (employee.getId()!=null) {
			service.update(employee,ids);
		}else{
			service.save(employee,ids);
		}
		return "redirect:/employee/list";
	}
}
